package testScripts;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import libraries.readData;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
//import org.testng.annotations.BeforeTest;
//import org.testng.annotations.Test;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;

public class AppLogin {

	public static Properties CONFIG = null;
	public static Properties OBJDESC = null;
	public static readData readXLData = null;
	public static String testCaseName = null, teststep = null;
	public static String LoginDataID = null;
	public static String keyword;
	public static String object;
	public static WebDriver webdriver = null;
	public static EventFiringWebDriver driver = null;
	public static RemoteWebDriver rdriver = null;
	public static String data;
	public static String xpath;
	public static String vBrowser, vUserName, vPassword, vMessage1, vMessage2 ;

	@BeforeClass
	
	public void Logon() throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		FileInputStream Config = new FileInputStream (System.getProperty("user.dir")+"\\src\\config\\testParameters.properties");
		CONFIG = new Properties();
		CONFIG.load(Config);

		FileInputStream objDesc = new FileInputStream (System.getProperty("user.dir")+"\\src\\config\\ObjectRepository.properties");
		OBJDESC = new Properties();
		OBJDESC.load(objDesc); 
	
		readXLData = new readData(System.getProperty("user.dir")+"\\src\\config\\LoginController.xlsx");
		System.out.println("-----------------------------------------------------------");
		System.out.println("App Login test Start");
		for (int i = 1; i<readXLData.getRowCount("TestCases"); i++){
			
			testCaseName = readXLData.getStringCellValue("TestCases", i, 1);
			if (readXLData.getStringCellValue("TestCases", i, 0).equals("Y")){
								
				System.out.println("Executing " + testCaseName+" "+ readXLData.getStringCellValue("TestCases", i, 2));
				
				for (int td = 1; td<readXLData.getRowCount("LoginData"); td++){
					
					if (readXLData.getStringCellValue("LoginData", td, 0).equals(testCaseName)){						
						System.out.println("Reading data from " +readXLData.getStringCellValue("LoginData", td, 1));
						
						LoginDataID = readXLData.getStringCellValue("TestData", td, 1);					   
						vUserName = readXLData.getStringCellValue("LoginData", td, 2);
						vPassword = readXLData.getStringCellValue("LoginData", td, 3);
						vMessage1 = readXLData.getStringCellValue("LoginData", td, 4);
						vMessage2 = readXLData.getStringCellValue("LoginData", td, 5); 
						for(int j = 1; j<readXLData.getRowCount(testCaseName); j++){
							teststep = readXLData.getStringCellValue(testCaseName, j, 1);
							System.out.println("Running Test step "+ teststep+" for "+ testCaseName);
							keyword = readXLData.getStringCellValue(testCaseName, j, 1);
							object = readXLData.getStringCellValue(testCaseName, j, 3);
							if(!(readXLData.getStringCellValue(testCaseName, j, 4)== "NA")){

								data = readXLData.getStringCellValue(testCaseName, j, 4);
								System.out.print("Data Input is "+ data + " ;");
								getIP();
							}
							System.out.print(" Object Name " + object+" ;");
							System.out.print(" Keyword is  " + keyword+" ;");
							System.out.println();
					
							Method method = AppLogin.class.getMethod(keyword);
							method.invoke(method);

						}// J loop
					} //td if execute

				} // td loop
			} // i if execute = Y
			else
			{
				System.out.println("Skipping " +testCaseName+" "+ readXLData.getStringCellValue("TestCases", i, 2));
			}



		} //-- i loop
		System.out.println("AppLogin test completed");
		System.out.println("-----------------------------------------------------------");
	}

	 @AfterClass
	  public void tearDown() throws Exception { 
	Thread.sleep(3000);
	
	try {
		driver.findElement(By.xpath(OBJDESC.getProperty("Button_LogOut_button"))).click();
		}   catch (Throwable t) {
			System.out.println("Object " + object + " Not found");
			
	     }
	
     driver.quit(); 
    } 
	//@Test(description="Gets input")
	public static void getIP(){

		if (data.equals("UserName")){
			data = vUserName;
		}

		if (data.equals("Password")){
			data = vPassword;
		}
		if (data.equals("Message1")){
			data = vMessage1;
		}
		if (data.equals("Message2")){
			data = vMessage2;
		}
	}

	//@Test(description="Opens Application")
	public static String OpenApp(){
		System.out.println("This is open app method");
		vBrowser = CONFIG.getProperty("browser");
		if(vBrowser.equals("IE")){					
		    File file = new File(CONFIG.getProperty("IEPath"));
			System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
			webdriver = new InternetExplorerDriver();
			driver = new EventFiringWebDriver(webdriver);	
		 }
		
		try {
			driver.navigate().to(CONFIG.getProperty(object));
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Unable to Open URL");
			return "Fail" + t.getMessage();
		}	
	}
	
	//@Test(description="Login to Application")
	public static String Login(){
		System.out.println("This is Login method");
		try {
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).sendKeys(CONFIG.getProperty(object));
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found");
			return "Fail" + t.getMessage();
		}

	}
	
	//@Test(description="Enters Input")
	public static String Enter_Input(){
		System.out.println("This is Enter Input method");
		try {
				driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
				driver.findElement(By.xpath(OBJDESC.getProperty(object))).clear();
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).sendKeys(data);
			driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found");
			return "Fail" + t.getMessage();
		}

	}

	//@Test(description="Click object")
	public static String Click(){
		System.out.println("This is click method");
		try {
			driver.findElement(By.xpath(OBJDESC.getProperty(object))).click();
			driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
			Thread.sleep(3000);
			return "Pass";
		} catch (Throwable t) {
			System.out.println("Object " + object + " Not found");
			return "Fail" + t.getMessage();
		}
	}


}
